package de.hwr.it11.dectree.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;

import de.hwr.it11.dectree.language.Messages;
import de.hwr.it11.dectree.model.DynamicTable;
import de.hwr.it11.dectree.model.Project;
import de.hwr.it11.dectree.model.Record;
import de.hwr.it11.dectree.persistence.FileManager;
import de.hwr.it11.dectree.persistence.ImportExportFailedException;

/**
 * Der <code>Controller</code> steuert die Applikationslogik.
 */
public class Controller {

	/** Das verwendete <code>Project</code> */
	private Project _project;



	/**
	 * Erzeugt einen neuen <code>Controller</code>.
	 * 
	 * @param pProject
	 *            das zu verwendende <code>Project</code>
	 */
	public Controller(Project pProject) {
		_project = pProject;
	}



	/**
	 * Importiert eine <code>DynamicTable</code> aus der Datei am angegebenen Dateipfad.
	 * 
	 * @param pPath
	 *            der Dateipfad zu der zu importierenden Datei
	 */
	public void importFile(String pPath) {
		List<List<String>> data = null;
		try {
			data = FileManager.getInstance().readFile(pPath);
			if (data.isEmpty()) {
				throw new ImportExportFailedException(Messages.ERROR_FILE_READ_EMPTY);
			}

			DynamicTable table = new DynamicTable();
			List<String> attributes = data.remove(0);
			for (String attribute : attributes) {
				if (attribute == null || attribute.isEmpty()) {
					throw new ImportExportFailedException(Messages.ERROR_FILE_READ_ATTRIBUTE_GAPS);
				}
				table.addAttribute(attribute);
			}

			for (int i = 0; i < data.size(); i++) {
				List<String> record = data.get(i);
				table.addRecord();
				for (int col = 0; col < record.size(); col++) {
					table.setValue(attributes.get(col), i, record.get(col));
				}
			}
			table.setGoalAttribute(table.getAttributeNames().get(table.getAttributeNames().size() - 1));

			_project.setDynamicTable(table);
		}
		catch (ImportExportFailedException | IOException e) {
			MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.DIALOG_FILE_READ_ERROR_TITLE,
					e.getLocalizedMessage());
		}
	}



	/**
	 * Export eine <code>DynamicTable</code> in die Datei am angegebenen Dateipfad.
	 * 
	 * @param pPath
	 *            der Dateipfad zu der zu exportierenden Datei
	 */
	public void exportFile(String pPath) {
		List<List<String>> data = new ArrayList<List<String>>();
		data.add(_project.getDynamicTable().getAttributeNames());
		for (Record record : _project.getDynamicTable().getTableData()) {
			List<String> values = new ArrayList<String>();
			for (String attribute : _project.getDynamicTable().getAttributeNames()) {
				values.add(record.get(attribute));
			}
			data.add(values);
		}

		try {
			FileManager.getInstance().writeFile(data, pPath);
		}
		catch (ImportExportFailedException | IOException e) {
			e.printStackTrace();
			MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.DIALOG_FILE_READ_ERROR_TITLE,
					e.getLocalizedMessage());
		}
	}

}
